package www.study.com;

//回文数 https://leetcode.cn/problems/palindrome-number/
public class code9 {
    public static void main(String[] args) {

    }

    /**
     * 负数直接false
     * 整数判断
     */
    class Solution {
        /**
         * 方法二：将x进行反转，如x=12345，反正得到整数y=54321，之后判断x是否等于y即可
         * @param x
         * @return
         */
        public boolean isPalindrome(int x) {
            return x >= 0 && reverseEq(x);
        }
        public boolean reverseEq(int x){
            int res = 0;
            int cur = x;
            while(x > 0){
                if(res > 214748364 || (res == 214748364 && (x % 10 > 7))){
                    return false;
                }
                res = res * 10 + x % 10;
                x /= 10;
            }
            return res == cur;
        }

        /**
         * 方法一：负数直接为false，整数转成字符串判断
         * @param x
         * @return
         */
        /*public boolean isPalindrome(int x) {
            if(x < 0) return false;
            char[] chs = String.valueOf(x).toCharArray();
            int l = 0;
            int r = chs.length - 1;
            while(l < r){
                if(chs[l] != chs[r]){
                    return false;
                }
                l++;
                r--;
            }
            return true;
        }*/
    }
}
